package com.yonyou.cyxdms.sysAuth.service.service.position;

import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.mapper.OrgMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.PositionMapper;
import com.yonyou.cyxdms.sysmanage.client.constants.BaseDataDictConstants;
import com.yonyou.cyxdms.sysmanage.client.vo.user.UserInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author     ：quankk
 * @date       ：2019/7/11 16:02
 * @description：
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PositionServiceImpl implements PositionService {

    // 定义日志接口
    private static final Logger logger = LoggerFactory.getLogger(PositionServiceImpl.class);

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public List<Map<String, Object>> findAllPosition() {
        return positionMapper.findAllPositionByIsValid(ManageDictCodeConstants.STATUS_IS_YES);
    }

    @Override
    public List<Integer> getPositions(Long userId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return positionMapper.getPositions(userId,loginInfoDto.getCompanyId());
    }

    /**
     * 根据orgId获得所有的人员信息
     *
     * @author 李贺
     * @since 2019/11/19
     */
    @Override
    public List<UserInfoVO> getUserByOrgId(String orgId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Long userOrgId = loginInfoDto.getUserOrgId();
        List<Integer> positionList = positionMapper.getPositionsByUserOrgId(userOrgId);
        List<UserInfoVO> userInfoVOList=new ArrayList<>();
        if (positionList.contains(ManageDictCodeConstants.SALES_AUDIT)) {
           String orgs= orgMapper.getOrgs(Long.parseLong(orgId));
            String[] orgIds = orgs.split(",");
            userInfoVOList = positionMapper.findUserByPositionAndOrgIds(BaseDataDictConstants.HALL_CONSULTANT, orgIds,null);
        } else if (positionList.contains(BaseDataDictConstants.HALL_CONSULTANT)) {
            userInfoVOList = positionMapper.findUserByPositionAndOrgIds(BaseDataDictConstants.HALL_CONSULTANT, null,loginInfoDto.getUserOrgId());
        }
        return userInfoVOList;
    }
}
